<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年级" prop="sjNjid">
        <el-input
          v-model="queryParams.sjNjid"
          placeholder="请输入年级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体测项目id" prop="sjTcxmid">
        <el-input
          v-model="queryParams.sjTcxmid"
          placeholder="请输入体测项目id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeSjCreate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="班级id" prop="sjBjid">
        <el-input
          v-model="queryParams.sjBjid"
          placeholder="请输入班级id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="sjSname">
        <el-input
          v-model="queryParams.sjSname"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tc:training:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tc:training:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tc:training:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tc:training:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="学生id" align="center" prop="sjXsid" />
      <el-table-column label="学校id" align="center" prop="sjXxid" />
      <el-table-column label="年级" align="center" prop="sjNjid">
        <template #default="scope">
          <dict-tag :options="tc_nj" :value="scope.row.sjNjid"/>
        </template>
      </el-table-column>
      <el-table-column label="体测项目id" align="center" prop="sjTcxmid" />
      <el-table-column label="体测分类id" align="center" prop="sjTcfl" />
      <el-table-column label="体测基础数据id" align="center" prop="sjJcsjid" />
      <el-table-column label="设备id" align="center" prop="sjSbid" />
      <el-table-column label="体测项目得分" align="center" prop="sjFs" />
      <el-table-column label="不及格 及格 良好 优秀" align="center" prop="sjDfjb" />
      <el-table-column label="体测项目的具体得分数据值" align="center" prop="sjDfsz" />
      <el-table-column label="体测时间" align="center" prop="sjTcsj" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sjTcsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="体测教师" align="center" prop="sjTcjs" />
      <el-table-column label="创建时间" align="center" prop="sjCreate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sjCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="sjCreateby" />
      <el-table-column label="修改时间" align="center" prop="sjUpdate" />
      <el-table-column label="修改人" align="center" prop="sjUpdateby" />
      <el-table-column label="身份证" align="center" prop="sjSfz" />
      <el-table-column label="学号" align="center" prop="sjXh" />
      <el-table-column label="班级id" align="center" prop="sjBjid" />
      <el-table-column label="学生姓名" align="center" prop="sjSname" />
      <el-table-column label="体测的原始数据" align="center" prop="sjData" />
      <el-table-column label="性别" align="center" prop="sjSex">
        <template #default="scope">
          <dict-tag :options="sys_user_sex" :value="scope.row.sjSex"/>
        </template>
      </el-table-column>
      <el-table-column label="体测数据单位" align="center" prop="sjUnit" />
      <el-table-column label="体测名称ID" align="center" prop="tcnameid" />
      <el-table-column label="体测照片表ID" align="center" prop="tcphotosid" />
      <el-table-column label="队列学生ID" align="center" prop="queuestudentid" />
      <el-table-column label="队列ID" align="center" prop="queueid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tc:training:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tc:training:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体测训练数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="trainingRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生id" prop="sjXsid">
          <el-input v-model="form.sjXsid" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="学校id" prop="sjXxid">
          <el-input v-model="form.sjXxid" placeholder="请输入学校id" />
        </el-form-item>
        <el-form-item label="年级" prop="sjNjid">
          <el-input v-model="form.sjNjid" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="体测项目id" prop="sjTcxmid">
          <el-input v-model="form.sjTcxmid" placeholder="请输入体测项目id" />
        </el-form-item>
        <el-form-item label="体测分类id" prop="sjTcfl">
          <el-input v-model="form.sjTcfl" placeholder="请输入体测分类id" />
        </el-form-item>
        <el-form-item label="体测基础数据id" prop="sjJcsjid">
          <el-input v-model="form.sjJcsjid" placeholder="请输入体测基础数据id" />
        </el-form-item>
        <el-form-item label="设备id" prop="sjSbid">
          <el-input v-model="form.sjSbid" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="体测项目得分" prop="sjFs">
          <el-input v-model="form.sjFs" placeholder="请输入体测项目得分" />
        </el-form-item>
        <el-form-item label="不及格 及格 良好 优秀" prop="sjDfjb">
          <el-input v-model="form.sjDfjb" placeholder="请输入不及格 及格 良好 优秀" />
        </el-form-item>
        <el-form-item label="体测项目的具体得分数据值" prop="sjDfsz">
          <el-input v-model="form.sjDfsz" placeholder="请输入体测项目的具体得分数据值" />
        </el-form-item>
        <el-form-item label="体测时间" prop="sjTcsj">
          <el-date-picker clearable
            v-model="form.sjTcsj"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择体测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体测教师" prop="sjTcjs">
          <el-input v-model="form.sjTcjs" placeholder="请输入体测教师" />
        </el-form-item>
        <el-form-item label="创建时间" prop="sjCreate">
          <el-date-picker clearable
            v-model="form.sjCreate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="sjCreateby">
          <el-input v-model="form.sjCreateby" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="sjUpdate">
          <el-input v-model="form.sjUpdate" placeholder="请输入修改时间" />
        </el-form-item>
        <el-form-item label="修改人" prop="sjUpdateby">
          <el-input v-model="form.sjUpdateby" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="身份证" prop="sjSfz">
          <el-input v-model="form.sjSfz" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="学号" prop="sjXh">
          <el-input v-model="form.sjXh" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="班级id" prop="sjBjid">
          <el-input v-model="form.sjBjid" placeholder="请输入班级id" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="sjSname">
          <el-input v-model="form.sjSname" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="体测的原始数据" prop="sjData">
          <el-input v-model="form.sjData" placeholder="请输入体测的原始数据" />
        </el-form-item>
        <el-form-item label="性别" prop="sjSex">
          <el-select v-model="form.sjSex" placeholder="请选择性别">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="体测数据单位" prop="sjUnit">
          <el-input v-model="form.sjUnit" placeholder="请输入体测数据单位" />
        </el-form-item>
        <el-form-item label="体测名称ID" prop="tcnameid">
          <el-input v-model="form.tcnameid" placeholder="请输入体测名称ID" />
        </el-form-item>
        <el-form-item label="体测照片表ID" prop="tcphotosid">
          <el-input v-model="form.tcphotosid" placeholder="请输入体测照片表ID" />
        </el-form-item>
        <el-form-item label="队列学生ID" prop="queuestudentid">
          <el-input v-model="form.queuestudentid" placeholder="请输入队列学生ID" />
        </el-form-item>
        <el-form-item label="队列ID" prop="queueid">
          <el-input v-model="form.queueid" placeholder="请输入队列ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Training">
import { listTraining, getTraining, delTraining, addTraining, updateTraining } from "@/api/tc/training";

const { proxy } = getCurrentInstance();
const { sys_user_sex ,tc_nj} = proxy.useDict('sys_user_sex','tc_nj');

const trainingList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeSjCreate = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sjNjid: null,
    sjTcxmid: null,
    sjCreate: null,
    sjBjid: null,
    sjSname: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询体测训练数据列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeSjCreate && '' != daterangeSjCreate) {
    queryParams.value.params["beginSjCreate"] = daterangeSjCreate.value[0];
    queryParams.value.params["endSjCreate"] = daterangeSjCreate.value[1];
  }
  listTraining(queryParams.value).then(response => {
    trainingList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    sjXsid: null,
    sjXxid: null,
    sjNjid: null,
    sjTcxmid: null,
    sjTcfl: null,
    sjJcsjid: null,
    sjSbid: null,
    sjFs: null,
    sjDfjb: null,
    sjDfsz: null,
    sjTcsj: null,
    sjTcjs: null,
    sjCreate: null,
    sjCreateby: null,
    sjUpdate: null,
    sjUpdateby: null,
    sjSfz: null,
    sjXh: null,
    sjBjid: null,
    sjSname: null,
    sjData: null,
    sjSex: null,
    sjUnit: null,
    tcnameid: null,
    tcphotosid: null,
    queuestudentid: null,
    queueid: null
  };
  proxy.resetForm("trainingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeSjCreate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加体测训练数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTraining(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改体测训练数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["trainingRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTraining(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTraining(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除体测训练数据编号为"' + _ids + '"的数据项？').then(function() {
    return delTraining(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tc/training/export', {
    ...queryParams.value
  }, `training_${new Date().getTime()}.xlsx`)
}

getList();
</script>
